package yagwl.client.view.demo;

import yagwl.client.model.ItemDTO;
import yagwl.client.model.demo.FilteredLaureateCache;
import yagwl.client.widget.FilteredTable;
import yagwl.client.widget.Section;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ImageCell;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FilteredLaureateView extends Composite {
  private final Section section = new Section("Nobel Laureates");
  private final FilteredLaureateCache listCache = new FilteredLaureateCache();
  private final FilteredTable filteredTable = new FilteredTable();
  private final CellTable<ItemDTO> cellTable;
  private FilteredTable.FilterLocation loc = FilteredTable.FilterLocation.Left;

  public final static String HistoryToken = "filteredList";

  public FilteredLaureateView() {
    listCache.setHistoryToken(HistoryToken);
    section.setDescription(new HTML(desc));
    section.setFootnote("Data from wikipedia; parsing errors possible.");

    // change orientation button
    Button button = new Button("Change Orientation");
    button.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        loc = loc == FilteredTable.FilterLocation.Left ? FilteredTable.FilterLocation.Top
            : FilteredTable.FilterLocation.Left;
        filteredTable.setFilterLocation(loc);
        filteredTable.renderUI();
      }
    });

    // filtered table
    cellTable = filteredTable.getPagedCellTable().getCellTable();
    initColumns();
    filteredTable.setFilterLocation(loc);
    filteredTable.setMinRowsAndHeightForScroll(10, "200px");
    filteredTable.setFilteredListCache(listCache);
    filteredTable.renderUI();

    // section content
    VerticalPanel vPanel = new VerticalPanel();
    vPanel.setWidth("100%");
    vPanel.add(button);
    vPanel.add(filteredTable);
    vPanel.setCellHorizontalAlignment(button,
        HasHorizontalAlignment.ALIGN_RIGHT);
    section.setContent(vPanel);
    initWidget(section);
  }

  private void initColumns() {
    // Year
    TextColumn<ItemDTO> yearColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("year");
      }
    };
    cellTable.addColumn(yearColumn, "Year");

    // Category
    TextColumn<ItemDTO> catColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("category");
      }
    };
    cellTable.addColumn(catColumn, "Category");

    // Photo
    ImageCell imageCell = new ImageCell();
    Column<ItemDTO, String> photoColumn = new Column<ItemDTO, String>(imageCell) {
      @Override
      public String getValue(ItemDTO item) {
        String p = item.get("photo");
        if (p != null && p.length() != 0)
          return p;
        return "/img/trpx.gif";
      }
    };
    cellTable.addColumn(photoColumn, "Photo");

    // Name
    LinkCell linkCell = new LinkCell();
    Column<ItemDTO, String> nameColumn = new Column<ItemDTO, String>(linkCell) {
      @Override
      public String getValue(ItemDTO item) {
        String a = "<a href=http://en.wikipedia.org" + item.get("nameLink")
            + " target=_blank>" + item.get("name") + "</a>";
        return a;
      }
    };
    cellTable.addColumn(nameColumn, "Name");

    // Country
    TextColumn<ItemDTO> conColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("country");
      }
    };
    cellTable.addColumn(conColumn, "Country");

    // Rationale
    TextColumn<ItemDTO> ratColumn = new TextColumn<ItemDTO>() {
      @Override
      public String getValue(ItemDTO item) {
        return item.get("rationale");
      }
    };
    cellTable.addColumn(ratColumn, "Rationale");
  }

  private class LinkCell extends AbstractCell<String> {
    // @Override
    // public void render(String value, Object key, SafeHtmlBuilder sb) {
    // sb.appendHtmlConstant(value);
    // }

    @Override
    public void render(Context context, String value, SafeHtmlBuilder sb) {
      sb.appendHtmlConstant(value);
    }
  }

  private static final String desc = "FilteredTable adds data filtering capability to PagedCellTable. "
      + "Filters can be placed either on the left or on top of filtered data. Click <b>Change "
      + "Orientation</b> button to switch orientation.<p/>";
}